﻿using Infrastructure;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.PubSubEvents;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using ViewsAndViewModels.Interfaces;
using ViewsAndViewModels.Views;

namespace ViewsAndViewModels.ViewModels
{
    public class AboutViewModel : ViewModelBase, IAboutViewModel
    {
        public DelegateCommand OkCommand { get; set; }
        public AboutViewModel(IAboutView view) : base(view)
        {
            OkCommand = new DelegateCommand(ExecuteOkCommand, CanExecuteOkCommand);
            IEventAggregator ea = ServiceLocator.Current.GetInstance<IEventAggregator>();
            ea.GetEvent<ChangingMainContentView>().Subscribe(ChangingContentViewHandler);
        }
        private void ChangingContentViewHandler(string obj)
        {
            OkCommand.RaiseCanExecuteChanged();
        }
        private static void ExecuteOkCommand()
        {
            IRegionManager rm = ServiceLocator.Current.GetInstance<IRegionManager>();
            if (rm != null)
            {
                var theView = rm.Regions[RegionNames.ContentRegion].GetView(ViewNames.MainViewName);
                rm.Regions[RegionNames.ContentRegion].Activate(theView);
                rm.Regions[RegionNames.ContentRegion].Context = ViewNames.MainViewName;
                IEventAggregator ea = ServiceLocator.Current.GetInstance<IEventAggregator>();
                ea.GetEvent<ChangingMainContentView>().Publish(ViewNames.MainViewName);
            }
        }
        private static bool CanExecuteOkCommand()
        {
            return true;
        }
    }
}
